#include "mainwindow.h"
#include "ui_mainwindow.h"

#define fixColunmCount 6

void MainWindow::initModelFormStringList(QStringList &list)
{

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    currentFile = new QLabel("当前文件：", this);
    cellPosLab = new QLabel("当前单元格：", this);
    cellTextLab = new QLabel("单位内容：", this);
    currentFile->setMinimumWidth(200);
    cellPosLab->setMinimumWidth(150);

    theModel = new QStandardItemModel(10, fixColunmCount, this);
    theSelectModel = new QItemSelectionModel(theModel);

    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelectModel);

    ui->statusbar->addWidget(currentFile);
    ui->statusbar->addWidget(cellPosLab);
    ui->statusbar->addWidget(cellTextLab);

    connect(theSelectModel, &QItemSelectionModel::currentChanged, this, [=](const QModelIndex &current, const QModelIndex &previous) {
        Q_UNUSED(previous)
        if (!current.isValid()) return;
        cellPosLab->setText(QString::asprintf("当前单元格：%d行，%d列", current.row(), current.column()));
        QStandardItem *item = theModel->itemFromIndex(current);
        cellTextLab->setText("单位内容："+item->text());
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_openAction_triggered()
{
    QString path = QCoreApplication::applicationDirPath();
    QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件夹", path, "数据文件(*.txt);;所有文件(*.*)");
    if (fileName.isEmpty()) return;

    QStringList contentList;
    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
        QTextStream stream(&file);
        ui->plainTextEdit->clear();

        while (!stream.atEnd()) {
            QString line = stream.readLine();
            ui->plainTextEdit->appendPlainText(line);
            contentList.append(line);
        }

        file.close();
        currentFile->setText("当前文件："+fileName);
    }

    initModelFormStringList(contentList);
}

